package com.wenmingliang.demo2.com.wenmingliang.demo2;

public class DeepCloneExample implements Cloneable{

    private int[] arr;

    public DeepCloneExample() {

        arr = new int[10];

        for(int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
    }

    public void set(int index, int value) {
        arr[index] = value;
    }

    public int get(int index) {
        return arr[index];
    }

    /**
     * 深克隆
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected DeepCloneExample clone() throws CloneNotSupportedException {
        DeepCloneExample deepCloneExample = (DeepCloneExample) super.clone();
        deepCloneExample.arr = new int[arr.length];
        for(int i = 0; i < arr.length; i++) {
            deepCloneExample.arr[i] = arr[i];
        }
        return deepCloneExample;


    }

    public static void main(String[] args) {
        DeepCloneExample c1 = new DeepCloneExample();
        DeepCloneExample c2 = null;


        try {
            c2 = c1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        c1.set(6,9999);
        System.out.println(c2.get(6));

    }


}
